package com.wbxqy.car_management_system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbxqy.car_management_system.entity.*;
import com.wbxqy.car_management_system.service.ICarRequestService;
import com.wbxqy.car_management_system.service.IRatingService;
import com.wbxqy.car_management_system.service.IUserService;
import com.wbxqy.car_management_system.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 评价表 前端控制器
 * </p>
 *
 * @author wbxqy
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/wbxqy/rating")
public class RatingController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRatingService ratingService;
    @Autowired
    private ICarRequestService carRequestService;

    @PostMapping
    public boolean addRating(@RequestHeader("Authorization") String token, @RequestBody Rating rating) {
        final User user = getUser(token);
        rating.setUserIdWbxqy(user.getUserIdWbxqy());
        rating.setDateWbxqy(LocalDateTime.now());
        // 状态设置为已经评论
        final CarRequest one = carRequestService.getById(rating.getRequestIdWbxqy());
        one.setIsCommentWbxqy("T");
        carRequestService.updateById(one);
        return ratingService.save(rating);
    }

    @GetMapping("/list")
    public List<Rating> getRating(@RequestHeader("Authorization") String token) {
        final User user = getUser(token);
        final QueryWrapper<Rating> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id_wbxqy", user.getUserIdWbxqy());
        return ratingService.list(queryWrapper);
    }

    @DeleteMapping("/{id}")
    public boolean deleteRating(@PathVariable Long id) {
        return ratingService.removeById(id);
    }

    @PutMapping
    public boolean updateRating(@RequestBody Rating rating) {
        return ratingService.updateById(rating);
    }

    @GetMapping("/{id}")
    public Rating getRatingById(@PathVariable Long id) {
        return ratingService.getById(id);
    }

    @GetMapping("/page")
    public Page<Rating> getAllRatingsPage(@RequestParam(required = false, defaultValue = "1") Integer page,
                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                          @RequestParam(required = false) String ratingId,
                                          @RequestParam(required = false) String userId,
                                          @RequestParam(required = false) String driverId,
                                          @RequestParam(required = false) Integer rating) {
        // 构造分页构造器
        final Page<Rating> pageInfo = new Page<>(page, pageSize);
        final QueryWrapper<Rating> queryWrapper = new QueryWrapper<>();

        // 添加过滤条件
        if (ratingId != null && !ratingId.isEmpty()) {
            queryWrapper.like("rating_id_wbxqy", ratingId);
        }
        if (userId != null && !userId.isEmpty()) {
            queryWrapper.like("user_id_wbxqy", userId);
        }
        if (driverId != null && !driverId.isEmpty()) {
            queryWrapper.like("driver_id_wbxqy", driverId);
        }
        if (rating != null) {
            queryWrapper.eq("rating_wbxqy", rating);
        }
        // 执行分页查询
        final Page<Rating> ratingPage = ratingService.page(pageInfo, queryWrapper);
        for(Rating R:ratingPage.getRecords()) {
            User user = null;
            CarRequest carRequest = null;
            if (R.getUserIdWbxqy() != null) {
                user = userService.getById(R.getUserIdWbxqy());
            }
            if (R.getRequestIdWbxqy() != null) {
                carRequest = carRequestService.getFullRequest(R.getRequestIdWbxqy());
            }
            R.setUser(user);
            R.setCarRequest(carRequest);
        }
        return pageInfo;
    }

    private User getUser(String token) {
        final JSONObject tokenObj = (JSONObject) JSON.parse(JwtTokenUtil.getUsernameFromJWT(token));
        return userService.getById(tokenObj.get("userIdWbxqy").toString());
    }
}
